/**
 * author:Administrator
 * date:2016/1/4
 * desc:TODO
 */

'use strict';
var assert = require('assert');
var dao = require('../dao/user.js');
var biz = {};
module.exports = biz;

biz.create = function*(json) {
  var retObj = yield dao.create(json.name);
  return {errcode: 0, user: retObj.dataValues};
};


/**
 * not good, query twice
 * @param id
 * @returns {{errcode: number, user: (*|values|string)}}
 */
biz.get = function*(id) {
  var retObj = yield dao.findById(id); // first query
  var obj = retObj.dataValues;

  // get books and add to user;
  if (retObj) {
    var books = yield retObj.getBooks(); // second query
    obj.books = [];
    for (var book of books) obj.books.push(book.dataValues);
  }
  return {errcode: 0, user: obj};
};

/**
 * bug : query duplicate the result,
 * @param id
 * @returns {{errcode: number, user}}
 */
biz.getV2 = function*(id) {
  var retObj = yield dao.findById_withbook(id);
  var obj = JSON.parse(JSON.stringify(retObj));
  return {errcode: 0, user: obj};
};
